/**
 * 游戏动画服务
 * 负责处理游戏中的动画效果
 */

export default {
  /**
   * 显示浮动文本效果
   * @param {string} text 要显示的文本
   * @param {string} type 文本类型 (normal, money, etc.)
   */
  showFloatingText(text, type = 'normal') {
    console.log(`浮动文本: ${text} (${type})`);
    
    // 创建浮动文本元素
    const floatingText = document.createElement('div');
    floatingText.className = `floating-text ${type}`;
    floatingText.textContent = text;
    
    // 设置随机位置
    const container = document.querySelector('.game-container');
    if (container) {
      const containerRect = container.getBoundingClientRect();
      const x = Math.random() * (containerRect.width - 100) + 50;
      const y = Math.random() * (containerRect.height - 100) + 50;
      
      floatingText.style.left = `${x}px`;
      floatingText.style.top = `${y}px`;
      
      // 添加到容器
      container.appendChild(floatingText);
      
      // 动画结束后移除元素
      setTimeout(() => {
        floatingText.remove();
      }, 2000);
    }
  },
  
  /**
   * 播放移动音效
   */
  playMoveSound() {
    // 这里可以添加实际的音效播放代码
    console.log('播放移动音效');
  },
  
  /**
   * 移动玩家 - 带动画效果
   * @param {Object} gameState 游戏状态
   * @param {number} playerIndex 玩家索引
   * @param {number} steps 移动步数
   * @param {Function} onMoveComplete 移动完成回调
   */
  movePlayer(gameState, playerIndex, steps, onMoveComplete) {
    const player = gameState.players[playerIndex];
    const oldPosition = player.position;
    const boardSize = gameState.boardCells.length;
    
    // 计算目标位置
    const targetPosition = (player.position + steps + boardSize) % boardSize;
    
    // 清空移动队列
    gameState.moveQueue = [];
    gameState.isMoving = true;
    
    // 创建移动队列
    if (steps > 0) {
      // 正向移动
      for (let i = 1; i <= steps; i++) {
        const nextPos = (oldPosition + i) % boardSize;
        gameState.moveQueue.push(nextPos);
      }
    } else if (steps < 0) {
      // 反向移动
      for (let i = 1; i <= Math.abs(steps); i++) {
        const nextPos = (oldPosition - i + boardSize) % boardSize;
        gameState.moveQueue.push(nextPos);
      }
    }
    
    // 开始移动动画
    this.processNextMove(gameState, playerIndex, targetPosition, oldPosition, onMoveComplete);
  },
  
  /**
   * 处理移动队列中的下一步
   * @param {Object} gameState 游戏状态
   * @param {number} playerIndex 玩家索引
   * @param {number} targetPosition 目标位置
   * @param {number} oldPosition 原始位置
   * @param {Function} onMoveComplete 移动完成回调
   */
  processNextMove(gameState, playerIndex, targetPosition, oldPosition, onMoveComplete) {
    if (gameState.moveQueue.length === 0) {
      // 移动完成
      gameState.isMoving = false;
      
      // 添加移动完成的视觉效果
      const player = gameState.players[playerIndex];
      this.showFloatingText(`${player.name}已到达${gameState.boardCells[player.position].name}`);
      
      // 如果经过起点，获得200元
      if (targetPosition < oldPosition && targetPosition !== 0) {
        gameState.players[playerIndex].money += 200;
        
        // 调用移动完成回调
        if (onMoveComplete) {
          onMoveComplete({
            passedStart: true,
            targetPosition
          });
        }
      } else {
        // 调用移动完成回调
        if (onMoveComplete) {
          onMoveComplete({
            passedStart: false,
            targetPosition
          });
        }
      }
      
      return;
    }
    
    // 获取下一个位置
    const nextPosition = gameState.moveQueue.shift();
    
    // 更新玩家位置
    gameState.players[playerIndex].position = nextPosition;
    
    // 播放移动音效
    this.playMoveSound();
    
    // 显示当前格子名称的浮动文本
    this.showFloatingText(gameState.boardCells[nextPosition].name);
    
    // 检查是否经过起点
    if (nextPosition === 0 && oldPosition !== 0) {
      gameState.players[playerIndex].money += 200;
      this.showFloatingText('+200元 (经过起点)', 'money');
    }
    
    // 动态调整移动速度 - 根据剩余步数调整
    const remainingSteps = gameState.moveQueue.length;
    const adjustedSpeed = gameState.moveSpeed - (remainingSteps * 10);
    const finalSpeed = Math.max(150, Math.min(adjustedSpeed, gameState.moveSpeed));
    
    // 延迟处理下一步移动
    setTimeout(() => {
      this.processNextMove(gameState, playerIndex, targetPosition, oldPosition, onMoveComplete);
    }, finalSpeed);
  }
};